clear

include "$scriptsdir/us_prepareincome.do"

gen     election_date = .
replace election_date = mdy(11,  2, 1948)  if year == 1948
replace election_date = mdy(11,  4, 1952)  if year == 1952
replace election_date = mdy(11,  6, 1956)  if year == 1956
replace election_date = mdy(11,  8, 1960)  if year == 1960
replace election_date = mdy(11,  3, 1964)  if year == 1964
replace election_date = mdy(11,  5, 1968)  if year == 1968
replace election_date = mdy(11,  7, 1972)  if year == 1972
replace election_date = mdy(11,  2, 1976)  if year == 1976
replace election_date = mdy(11,  4, 1980)  if year == 1980
replace election_date = mdy(11,  6, 1984)  if year == 1984
replace election_date = mdy(11,  8, 1988)  if year == 1988
replace election_date = mdy(11,  3, 1992)  if year == 1992
replace election_date = mdy(11,  5, 1996)  if year == 1996
replace election_date = mdy(11,  7, 2000)  if year == 2000
replace election_date = mdy(11,  2, 2004)  if year == 2004
replace election_date = mdy(11,  4, 2008)  if year == 2008
replace election_date = mdy(11,  6, 2012)  if year == 2012


gen election_year_weight = doy(election_date)/365
foreach varn of varlist inc_* wtid_incgrowth* share* {
  gen orig_`varn' = `varn'
  replace `varn' = election_year_weight * orig_`varn' + (1-election_year_weight) * l.orig_`varn' if election_year_weight != .
  *replace `varn' = l.`varn' if halfyear(election_date) == 1
}


//// Scatter plot of mean and t5 growth rates
// Do this before merging in the ANES cumulative data file as this will remove non-election years
gen inc_ti_top5_m_g1_100 =  inc_ti_top5_m_g1 * 100
*gen inc_fti_mean_g1_100 = inc_fti_mean_g1 * 100
gen inc_ti_mean_g1_100 = inc_ti_mean_g1 * 100
// N.b. We exclude the 1993 growth rate as an outlier that seems very likely to be driven by a change in survey questions.  See footnote 23 of <https://www.census.gov/hhes/www/income/data/historical/ftnotes.html>
twoway (function y=x, range(-10 10)) (scatter inc_ti_top5_m_g1_100 inc_ti_mean_g1_100 if election_year == 0 & year != 1993, msymbol(oh)) (scatter inc_ti_top5_m_g1_100 inc_ti_mean_g1_100 if election_year == 1, msymbol(o) mcolor(gs4) yline(0, lstyle(dot)) xline(0, lstyle(dot))), scheme(s1mono) xtitle("Mean income growth (%)") ytitle("Top-5% income growth (%)") xlabel(-10(4)10) ylabel(-10(4)10) legend(off)
graph export "$imagedir/us_scatter_mean_t5.pdf", replace
window manage close graph

cor inc_ti_top5_m_g1_100 inc_ti_mean_g1_100 if year != 1993
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/us_rho_mean_t5.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}
cor inc_ti_top5_m_g1_100 inc_ti_mean_g1_100 if election_year == 1
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/us_rho_mean_t5_eyears.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}


gen            share5_x_T5g1 =	share_t5 * inc_ti_top5_m_g1
label variable share_t5			"$ Share^{T5}_t $"
label variable share5_x_T5g1	"$ Share^{T5}_{t} \times Growth^{T5}_{t} $"


include "$scriptsdir/us_preparecdf.do"

drop if year < 1952

// Match the variable labels to those of Bartels' tables (as much as possible given character limits).
label variable incvote 				"$ VoteIncumbent_{i,t} $"
label variable vote_ce 				"$ VoteIncumbent_{i,t} $"
*label variable inc_disp_g1			"$ Growth^{M}_{t} $"
label variable incumbent_tenure		"$ Tenure_{t} $"
label variable logtenure			"$ \log Tenure_{t} $"
label variable incpid 				"$ ProPartyID_{i,t} $"

label variable inc_ti_quin1_ul_g1	"$ Growth^{P20}_{t} $"
label variable inc_ti_quin2_ul_g1	"$ Growth^{P40}_{t} $"
label variable inc_ti_quin3_ul_g1	"$ Growth^{P60}_{t} $"
label variable inc_ti_quin4_ul_g1	"$ Growth^{P80}_{t} $"
label variable inc_ti_top5_ll_g1	"$ Growth^{P95}_{t} $"
label variable inc_ti_quin23_ul_g1	"$ Growth^{P40-60}_{t} $"

label variable inc_ti_quin1_m_g1	"$ Growth^{Q1M}_{t} $"
label variable inc_ti_quin2_m_g1	"$ Growth^{Q2M}_{t} $"
label variable inc_ti_quin3_m_g1	"$ Growth^{Q3M}_{t} $"
label variable inc_ti_quin4_m_g1	"$ Growth^{Q4M}_{t} $"
label variable inc_ti_top5_m_g1		"$ Growth^{T5M}_{t} $"
label variable inc_ti_quin12_m_g1	"$ Growth^{Q1-2M}_{t} $"

label variable inc_ti_mean_g1		"$ Growth^{M}_{t} $"
label variable inc_pti_mean_g1		"$ Growth^{M}_{t} $"
label variable inc_fti_mean_g1		"$ Growth^{M}_{t} $"


gen     altincpid = -1 if incpid < 0
replace altincpid =  0 if incpid == 0
replace altincpid =  1 if incpid > 0
label variable altincpid 				"$ ProPartyID^{Alt}_{i,t} $"


// Limit the sample to those who voted
*keep if VCF0703 == 3 // This variable doesn't seem to have values for 1948
keep if VCF0702 == 2

*cd "./empirics/"
global estout_options          label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(        ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0)   labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) p(fmt(2)) " ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se     label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(        ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0)   labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_xt_w_se  label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(sigma_u ll bic      N N_g     xtercsamp, fmt(3 0 0 0 0) labels("$ \sigma_u $" "Log Likelihood" "BIC"                  "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_ols_w_se label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2 N xtercsamp, fmt(2 0 0) labels("$ R^2 $" "N" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )


//// Estimate some aggregated models by ols - so excluding PID.
preserve
*collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 share5 [pweight=weight_eqelec], by(year)
collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_ti_mean_g1  inc_ti_top5_m_g1 share5 [pweight=weight_eqelec], by(year)
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1 incumbent_dem
reg incpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "All"
estimates store PIDOLSAll
reg altincpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "All"
estimates store PIDaltOLSAll
restore

preserve
*collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, by(year)
collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, by(year)
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1 incumbent_dem
reg incpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "Low"
estimates store PIDOLSLow
reg altincpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "Low"
estimates store PIDaltOLSLow
restore

preserve
*collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, by(year)
collapse (mean) vote_ce incumbent_tenure incumbent_dem logtenure incpid altincpid inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, by(year)
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
reg vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1 incumbent_dem
reg incpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "Mid"
estimates store PIDOLSMid
reg altincpid logtenure inc_ti_mean_g1  inc_ti_top5_m_g1
estadd local xtercsamp = "Mid"
estimates store PIDaltOLSMid
restore

estout  PIDOLSAll PIDOLSLow PIDOLSMid		using "$texdir/us_pid.tex",	$estout_options_ols_w_se order(logtenure inc_ti_mean_g1 inc_ti_top5_m_g1)
estout  PIDaltOLSAll PIDaltOLSLow PIDaltOLSMid		using "$texdir/us_pid_alt.tex",	$estout_options_ols_w_se order(logtenure inc_ti_mean_g1 inc_ti_top5_m_g1)



//// Section of models that we have decided to include in the main text of CBEVComparative

probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenure
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLow
probit vote_ce logtenure incpid inc_ti_quin1_ul_g1   inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1Low
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMid
probit vote_ce logtenure incpid inc_ti_quin23_ul_g1   inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3Mid
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_quin23_ul_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3Mid

*estout  BartelsLogtenure PreferredLow PreferredQ1Low MeanQ1Low PreferredMid PreferredQ3Mid MeanQ3Mid		using "$texdir/us_cbevcomp_tableA.tex",	$estout_options order(logtenure incpid inc_disp_g1 inc_ul_q1_g1 inc_ul_q3_g1 inc_ll_top5_g1)
estout  BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid		using "$texdir/us_cbevcomp_tableA.tex",	$estout_options_w_se order(logtenure incpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1)


// An identifier so we can summarize based on year not individual-level sample size
bys year: gen firstob = 1 if _n == 1
replace firstob = 0 if firstob == .


// Some code to generate predicted probabilities of incumbent vote to illustrate magnitudes in the paper.
estimates restore PreferredLow
summ inc_ti_top5_ll_g1 if firstob == 1
margins, at(inc_ti_top5_ll_g1 =(.015 .043)) asobserved

//// Perform various CBEV tests for the models above, and output the results to text files for direct inclusion into ".tex" tables.
estimates restore BartelsLogtenure
test inc_ti_top5_ll_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/us_BartelsLogtenure_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
}

estimates restore PreferredLow
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*test (inc_meanb80_g1/(1-`mean_share5')) = (inc_ll_top5_g1/`mean_share5')
test inc_ti_top5_ll_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/us_PreferredLow_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "us/us_PreferredLow_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredQ1Low
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*test (inc_ul_q1_g1/(1-`mean_share5')) = (inc_ll_top5_g1/`mean_share5')
*test inc_ul_q1_g1 = inc_ll_top5_g1
test inc_ti_quin1_ul_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/us_PreferredQ1Low_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "us/us_PreferredQ1Low_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredQ3Mid
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*test (inc_ul_q1_g1/(1-`mean_share5')) = (inc_ll_top5_g1/`mean_share5')
*test inc_ul_q1_g1 = inc_ll_top5_g1
test inc_ti_quin23_ul_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/us_PreferredQ3Mid_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "us/us_PreferredQ1Low_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}

estimates restore PreferredMid
*quietly summ share5 if e(sample)
*local mean_share5 = r(mean)/100
*test (inc_meanb80_g1/(1-`mean_share5')) = (inc_ll_top5_g1/`mean_share5')
test inc_ti_top5_ll_g1
local pstat =  string(r(p), "%9.2fc")
*local chi2stat = string(r(chi2), "%9.2fc")
capture {
  file open  pstatfile using "$texdir/us_PreferredMid_pstat.tex", write replace
  file write pstatfile "`pstat'"
  file close pstatfile
  *file open  chi2statfile using "us/us_PreferredMid_chi2stat.tex", write replace
  *file write chi2statfile "`chi2stat'"
  *file close chi2statfile
}


//// Section of models that we present in supplementary material

probit vote_ce incumbent_tenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store Bartels

estout  Bartels BartelsLogtenure		using "$texdir/us_cbevcomp_tableB.tex",	$estout_options_w_se order(incumbent_tenure logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1)


// Re-estimate the full set of main-text models, but with the simpler pid measure

probit vote_ce logtenure altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureAPID
probit vote_ce logtenure altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowAPID
probit vote_ce logtenure altincpid inc_ti_quin1_ul_g1   inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowAPID
probit vote_ce logtenure altincpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowAPID
probit vote_ce logtenure altincpid inc_fti_mean_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidAPID
probit vote_ce logtenure altincpid inc_ti_quin23_ul_g1   inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidAPID
probit vote_ce logtenure altincpid inc_fti_mean_g1 inc_ti_quin23_ul_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidAPID

*estout  BartelsLogtenure PreferredLow PreferredQ1Low MeanQ1Low PreferredMid PreferredQ3Mid MeanQ3Mid		using "$texdir/us_cbevcomp_tableA.tex",	$estout_options order(logtenure incpid inc_disp_g1 inc_ul_q1_g1 inc_ul_q3_g1 inc_ll_top5_g1)
estout  BartelsLogtenureAPID PreferredLowAPID PreferredQ1LowAPID PreferredMidAPID PreferredQ3MidAPID		using "$texdir/us_cbevcomp_tableC.tex",	$estout_options_w_se order(logtenure altincpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1)


probit vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoPID
probit vote_ce logtenure inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoPID
probit vote_ce logtenure inc_ti_quin12_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowNoPID
probit vote_ce logtenure inc_ti_mean_g1 inc_ti_quin12_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowNoPID
probit vote_ce logtenure inc_ti_mean_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoPID
probit vote_ce logtenure inc_ti_quin3_m_g1   inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidNoPID
probit vote_ce logtenure inc_ti_mean_g1 inc_ti_quin3_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidNoPID

*estout  BartelsLogtenure PreferredLow PreferredQ1Low MeanQ1Low PreferredMid PreferredQ3Mid MeanQ3Mid		using "$texdir/us_cbevcomp_tableA.tex",	$estout_options order(logtenure incpid inc_disp_g1 inc_ul_q1_g1 inc_ul_q3_g1 inc_ll_top5_g1)
estout  BartelsLogtenureNoPID PreferredLowNoPID PreferredQ1LowNoPID PreferredMidNoPID PreferredQ3MidNoPID		using "$texdir/us_cbevcomp_tableD.tex",	$estout_options_w_se order(logtenure inc_ti_mean_g1 inc_ti_quin12_m_g1 inc_ti_quin3_m_g1 inc_ti_top5_m_g1)


// Robustness for JoP-R2.  What happens when we use `own'-group growth, mean growth, and top growth?
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store OwnMeanLow
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store OwnMeanMid
probit vote_ce logtenure incpid inc_ti_quin12_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ12Low
probit vote_ce logtenure incpid inc_ti_quin1_ul_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low


estout  PreferredQ1Low		using "$texdir/us_cbevcomp_tableE.tex",	$estout_options_w_se order(logtenure incpid inc_ti_quin1_ul_g1 inc_ti_top5_m_g1)


probit vote_ce logtenure incpid inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store T5M1
probit vote_ce logtenure incpid inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5M2
probit vote_ce logtenure incpid inc_ti_quin12_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5M3
probit vote_ce logtenure incpid inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5M4
probit vote_ce logtenure incpid inc_ti_quin3_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5M5

estout  T5M1 T5M2 T5M3 T5M4 T5M5		using "$texdir/us_cbevcomp_tableF.tex",	$estout_options_w_se order(logtenure incpid inc_ti_mean_g1 inc_ti_quin12_m_g1 inc_ti_quin3_m_g1 inc_ti_top5_m_g1)



xtset year
xtprobit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1
estadd local xtercsamp = "All"
estadd scalar N_clust = e(N_g)
estimates store XTBartelsLogtenure
xtprobit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 if income3 == 0
estadd local xtercsamp = "Low"
estadd scalar N_clust = e(N_g)
estimates store XTPreferredLow
xtprobit vote_ce logtenure incpid inc_ti_quin1_ul_g1   inc_ti_top5_ll_g1 if income3 == 0
estadd local xtercsamp = "Low"
estadd scalar N_clust = e(N_g)
estimates store XTPreferredQ1Low
xtprobit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_ll_g1 if income3 == 1
estadd local xtercsamp = "Mid"
estadd scalar N_clust = e(N_g)
estimates store XTPreferredMid
xtprobit vote_ce logtenure incpid inc_ti_quin23_ul_g1   inc_ti_top5_ll_g1 if income3 == 1
estadd local xtercsamp = "Mid"
estadd scalar N_clust = e(N_g)
estimates store XTPreferredQ3Mid

estout  XTBartelsLogtenure XTPreferredLow XTPreferredQ1Low XTPreferredMid XTPreferredQ3Mid		using "$texdir/us_cbevcomp_tableG.tex",	drop(_cons) $estout_options_xt_w_se order(logtenure incpid inc_fti_mean_g1 inc_ti_quin1_ul_g1 inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1)


probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_m_g1 share_t5 share5_x_T5g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureS5
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_m_g1 share_t5 share5_x_T5g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowS5
probit vote_ce logtenure incpid inc_fti_mean_g1 inc_ti_top5_m_g1 share_t5 share5_x_T5g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidS5

estout  BartelsLogtenureS5 PreferredLowS5 PreferredMidS5		using "$texdir/us_cbevcomp_tableH.tex",	$estout_options_w_se order(logtenure incpid inc_fti_mean_g1 share_t5 inc_ti_top5_m_g1 share5_x_T5g1)



probit vote_ce logtenure        inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store T5M1NoPID
probit vote_ce logtenure        inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5M2NoPID
probit vote_ce logtenure        inc_ti_quin1_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5M3NoPID
probit vote_ce logtenure        inc_ti_mean_g1  inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5M4NoPID
probit vote_ce logtenure        inc_ti_quin3_m_g1 inc_ti_top5_m_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5M5NoPID

estout  T5M1NoPID T5M2NoPID T5M3NoPID T5M4NoPID T5M5NoPID		using "$texdir/us_cbevcomp_tableI.tex",	$estout_options_w_se order(logtenure inc_ti_mean_g1 inc_ti_quin1_m_g1 inc_ti_quin3_m_g1 inc_ti_top5_m_g1)


probit vote_ce logtenure        inc_fti_mean_g1  inc_ti_top5_ll_g1 [pweight=weight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store P951NoPID
probit vote_ce logtenure        inc_fti_mean_g1  inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store P952NoPID
probit vote_ce logtenure        inc_ti_quin1_ul_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store P953NoPID
probit vote_ce logtenure        inc_fti_mean_g1  inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store P954NoPID
probit vote_ce logtenure        inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1 [pweight=weight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store P955NoPID

estout  P951NoPID P952NoPID P953NoPID P954NoPID P955NoPID		using "$texdir/us_cbevcomp_tableJ.tex",	$estout_options_w_se order(logtenure inc_fti_mean_g1 inc_ti_quin1_ul_g1 inc_ti_quin23_ul_g1 inc_ti_top5_ll_g1)



gen model_country = "USA"
gen model_name = ""
gen predicted_diff_mean_m = .
gen predicted_diff_mean_se = .
gen predicted_diff_t5_m = .
gen predicted_diff_t5_se = .
local counter = 1
foreach model in BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid {
  estimates restore `model'
  summ inc_ti_top5_ll_g1 if e(sample)
  local t5_mean =  r(mean)
  local t5_sd   =  r(sd)
  local t5_mean_sd = `t5_mean' + `t5_sd'
  margins, pwcompare at((asobserved) _all inc_ti_top5_ll_g1=(`t5_mean' `t5_mean_sd'))
  matrix pdiff_m = r(b_vs)
  matrix pdiff_v = r(V_vs)
  scalar pdiff_m = pdiff_m[1,1]
  scalar pdiff_se = pdiff_v[1,1]^(1/2)
  replace model_name = "`model'" if _n == `counter'
  replace predicted_diff_t5_m = pdiff_m if _n == `counter'
  replace predicted_diff_t5_se = pdiff_se if _n == `counter'
  // Now mean version
  if "`model'" == "BartelsLogtenure" | "`model'" == "PreferredLow" | "`model'" == "PreferredMid" {
    summ inc_pti_mean_g1 if e(sample)
    local mean_mean =  r(mean)
    local mean_sd   =  r(sd)
    local mean_mean_sd = `mean_mean' + `t5_sd'
    margins, pwcompare at((asobserved) _all inc_fti_mean_g1=(`mean_mean' `mean_mean_sd'))
    matrix pdiff_m = r(b_vs)
    matrix pdiff_v = r(V_vs)
    scalar pdiff_m = pdiff_m[1,1]
    scalar pdiff_se = pdiff_v[1,1]^(1/2)
    replace model_name = "`model'" if _n == `counter'
    replace predicted_diff_mean_m = pdiff_m if _n == `counter'
    replace predicted_diff_mean_se = pdiff_se if _n == `counter'
  }
  // Increment counter
  local counter = `counter' + 1
}
keep model_country model_name predicted_diff_*
drop if model_name == ""
save "$gendatadir/us_predicted_differences.dta", replace

